var appData = getApp().globalData;
var appReq = getApp().request;
var form = { name: '', phone: '' };
var Url = 'login/';
var newer = 0, hotel;
Page({
  data: {
    disabled: true,
  },
  /*
  radioChange: function (e) {
    form['性别'] = e.detail.value
    console.log(form)
  },
  */
  inp: function(e) {
    form[e.currentTarget.id] = e.detail.value;
    if (form.name == '' || form.phone.length != 11) {
      this.setData({
        disabled: true
      })
    } else {
      this.setData({
        disabled: false
      })
    }
  },
  reg: function(r) {
    var i = r.detail.userInfo,
      that = this;
    if (!i) return 0;
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[4-9]{1})|(18[0-9]{1})|(19[8-9]{1}))+\d{8})$/;
    if (!myreg.test(form.phone)) {
      wx.showModal({
        title: '',
        content: '为保证优惠券顺利发放，请填写正确的手机号码哦~',
        showCancel: false,
        confirmText: '好的'
      });
    } else {
      wx.showModal({
        title: ' 确认信息',
        content: '您的手机号码为' + form.phone + '，信息确认后不能修改了哦~',
        success(tap) {
          if (tap.confirm) {
            wx.showLoading({
              title: '正在发送请求'
            })
            that.sendReq(i);
          }
        }
      })
    }
  },
  sendReq: function(i) {
    var REQ_NOTE = 'Register hotel guest info';
    form.nick = i.nickName;
    form.city = i.city;
    form.gender = i.gender;
    form.avatar = i.avatarUrl;
    form.openid = appData.user.openid;
    appReq(Url + 'register', form, REQ_NOTE).then((res) => {
      console.log(res.name, res.data.user, res.data.share);
      appData.user = res.data.user;
      appData.share = res.data.share;
      if (!appData.share.total) appData.share.total = parseFloat("0").toFixed(2)
      REQ_NOTE = 'Register global manage info';
      var str = parseInt(newer) ? '' : hotel + ',';
      var data = {
        newer: newer,
        openid: form.openid,
        hotels: str + appData.hotel + ':' + appData.user.id,
      };
      appReq(Url + 'reg_global', data, REQ_NOTE).then((res) => {
        console.log(res.name, res.data);
        wx.showToast({
          title: '注册成功',
          icon: 'success'
        })
        wx.reLaunch({
          url: '../index/index'
        })

      })
    })
  },

  onLoad: function(pre) {
    newer = pre.newer;
    hotel = pre.hotel;
    console.log('newer:', newer, ' hotel:', hotel);
  },
})